package com.jianhua.appmarket.module.service.system.impl;

import com.jianhua.appmarket.common.util.AssertBusiness;
import com.jianhua.appmarket.common.util.SnowflakeKeyUtil;
import com.jianhua.appmarket.module.dao.system.RoleUserDao;
import com.jianhua.appmarket.module.entity.system.RoleMenu;
import com.jianhua.appmarket.module.entity.system.RoleUser;
import com.jianhua.appmarket.module.model.system.RoleUserParam;
import com.jianhua.appmarket.module.model.system.UserVO;
import com.jianhua.appmarket.module.service.system.IRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleUserService implements IRoleUserService {

    @Autowired
    private RoleUserDao roleUserDao;


    @Override
    public Boolean saveRoleUsers(RoleUserParam param, UserVO user) throws Exception {
        Long userId = param.getUserId();
        AssertBusiness.isTrue(0 < userId,"请先选择一个用户!");

        //先删除再添加
        RoleUser deleteParam = new RoleUser();
        deleteParam.setUserId(userId);
        roleUserDao.deleteByParam(deleteParam);

        RoleUser roleUser =new RoleUser();
        for(Long roleId : param.getRoleIds()){
            roleUser.setId(SnowflakeKeyUtil.generateId());
            roleUser.setRoleId(roleId);
            roleUser.setUserId(userId);
            roleUser.setCreateUser(user.getNickName());
            roleUser.setUpdateUser(user.getNickName());
            roleUser.setUpdateTime(new Date());
            roleUserDao.insert(roleUser);
        }
        return Boolean.TRUE;
    }

    @Override
    public List<RoleUser> listByUserId(Long userId) {
        RoleUser param = new RoleUser();
        param.setUserId(userId);
        return roleUserDao.listByParam(param);
    }
}
